﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SortMethods
{
    class Program
    {
        //https://www.itcodemonkey.com/article/6633.html
        //https://www.itcodemonkey.com/article/6409.html
        static int[] example = new int[10] { 4, 32, 1, 5, 3, 21, 6, 43, 41, 34 };
        static void Main(string[] args)
        {
            //var p = 0;
            //for(int index=1;index<=100;index++)
            //{
            //    if(index.ToString().Contains("8"))
            //    {
            //        p++;
            //        Console.Write(index + " ");
            //    }
            //}
            //Console.WriteLine($"包含有8的有{p}个");
            BubbleSort();
            SelectionSort();
            InsertionSort();
            Console.ReadKey();
        }
        static void swapData(int i, int j)
        {
            var data = i;
            i = j;
            j = data;
        }
        static void InsertionSort()
        {
            var Example = new int[10];
            example.CopyTo(Example, 0);
            Console.WriteLine("排序前\r\n");
            foreach (var item in Example)
            {
                Console.Write(item + " ");
            }
            Console.WriteLine("\r\n");

            for (int index = 1; index < Example.Length; index++)
            {
                var data = Example[index];
                int count = 0;
                for (count = index - 1; count >= 0 && data < Example[count]; count--)
                    Example[count + 1] = Example[count];
                Example[count + 1] = data;
            }


            Console.WriteLine("排序后\r\n");
            foreach (var item in Example)
            {
                Console.Write(item + " ");
            }
            Console.WriteLine("\r\n");
        }

        /// <summary>
        /// 选择排序
        /// </summary>
        static void SelectionSort()
        {
            var Example = new int[10];
            example.CopyTo(Example, 0);
            Console.WriteLine("排序前\r\n");
            foreach (var item in Example)
            {
                Console.Write(item + " ");
            }
            Console.WriteLine("\r\n");
            for (int index = 0; index < Example.Length; index++)
            {
                int min = index;
                for (int count = index; count < Example.Length; count++)
                {
                    if (Example[count] < Example[min])
                        min = count;
                }
                var data = Example[index];
                Example[index] = Example[min];
                Example[min] = data;
            }
            Console.WriteLine("排序后\r\n");
            foreach (var item in Example)
            {
                Console.Write(item + " ");
            }
            Console.WriteLine("\r\n");
        }
        /// <summary>
        /// 冒泡
        /// </summary>
        static void BubbleSort()
        {
            var Example = new int[10];
            example.CopyTo(Example, 0);
            Console.WriteLine("排序前\r\n");
            foreach (var item in Example)
            {
                Console.Write(item + " ");
            }
            Console.WriteLine("\r\n");
            for (int index = 0; index < Example.Length; index++)
            {
                for (int count = index; count < Example.Length; count++)
                {
                    if (Example[index] > Example[count])
                    {
                        var b = Example[count];
                        Example[count] = Example[index];
                        Example[index] = b;
                    }
                }
            }
            Console.WriteLine("排序后\r\n");
            foreach (var item in Example)
            {
                Console.Write(item + " ");
            }
            Console.WriteLine("\r\n");
        }
    }
}
